package com.songlanyun.modules.rabbitmq.mq.consumer;

import com.alipay.api.domain.Sale;
import com.rabbitmq.client.Channel;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.WxConstant;
import com.songlanyun.common.utils.StrUtil;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.account.service.UserThirdIdentityService;
import com.songlanyun.modules.notifytemp.model.dto.WxNotifyDTO;
import com.songlanyun.modules.notifytemp.service.NotifyTempService;
import com.songlanyun.modules.order.entity.Order;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.ship.entity.OrderPackage;
import com.songlanyun.modules.ship.entity.OrderPackageExpress;
import com.songlanyun.modules.ship.entity.OrderPackageItem;
import com.songlanyun.modules.ship.entity.OrderPackagePickup;
import com.songlanyun.modules.ship.service.OrderPackageItemService;
import com.songlanyun.modules.ship.service.OrderPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 普通消费物流订单,发送成功发送消目秀眉清
 */
@Component
@Slf4j
public class ShipSuccessSendMessageConsumer extends BaseConsumer {

    @Autowired
    private OrderPackageService orderPackageService;

    @Autowired
    private NotifyTempService notifyTempService;

    @Autowired
    private UserThirdIdentityService userThirdIdentityService;
    @Autowired
    private OrderPackageItemService orderPackageItemService;

    @Override
    protected void process(Message message) {
        Long packageId = ClassConvertUtils.objToLong(new String(message.getBody()));
        OrderPackage v = orderPackageService.getById(packageId);
        List<OrderPackageItem> orderPackageItems = orderPackageItemService.loadOrderPackageItem(packageId);
        Order order = v.loadOrder();

        try {
            String openId = userThirdIdentityService.getOpenIdByUserId(order.getUserId(), false);
            if (OrderConstant.ChannelType.WE_APPLET.equals(order.getChannelType())) {
                //自提包裹也需要物流通知
                if (v.getGoodsPackageType().equals(OrderConstant.GoodsPackageType.EXPRESS)) {
                /**
                 * 订单号
                 * 商品名称
                 * 物流名称
                 * 物流单号
                 * 状态
                 */
                String shortGoodsTitle = orderPackageItems.get(0).getGoodsTitle();//用标题
                int sum = orderPackageItems.stream().mapToInt(OrderPackageItem::getNumber).sum();
                shortGoodsTitle = StrUtil.getNumStr(shortGoodsTitle, 15) + "等" + sum + "件";

                String msg = "";
                String expressName = "";
                String logisticCode = "";

                if (OrderConstant.GoodsPackageStatus.WAIT_RECEIPT == v.getGoodsPackageStatus()
                        && OrderConstant.GoodsPackageType.PICKUP == v.getGoodsPackageType()) {
                    //备货
                    OrderPackagePickup orderPackagePickup = v.loadPickup();
                    msg = "已备货完成";
                    expressName = order.getShopName();
                    logisticCode = orderPackagePickup.getVerificationCode();
                } else {
                    //发货
                    OrderPackageExpress orderPackageExpress = v.loadExpress();
                    msg = "已发货";
                    expressName = orderPackageExpress.getExpressName();
                    logisticCode = orderPackageExpress.getLogisticCode();
                }

                List<Object> values = new ArrayList<>();
                values.add(v.getOrderCode());
                values.add(shortGoodsTitle);
                values.add(expressName);
                values.add(logisticCode);
                values.add(msg);
                WxNotifyDTO wxNotifyDTO = new WxNotifyDTO(WxConstant.WxType.LOGISTICS_INFORM, openId, values);
                notifyTempService.sendWxNotify(wxNotifyDTO);
                }
            }

        } catch (Exception ex) {
            throw ex;
        }

    }

    @RabbitListener(queues = MqConstant.ShipSuccessSendMessageConstant.SHIP_SUCCESS_SEND_MESSAGE_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {
        super.handle(message, channel);
    }
}
